package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result page(StudentQueryParam param){
        log.info("分页查询学生信息，参数：{}", param);
        PageResult<Student> pageResult = studentService.page(param);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids){
        log.info("删除学生信息，id：{}", Arrays.asList(ids));
        studentService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result insert(@RequestBody Student student){
        log.info("新增学生信息，参数：{}", student);
        studentService.insert(student);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改学生信息，参数：{}", student);
        studentService.update(student);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("查询学生信息，id：{}", id);
        Student student = studentService.selectById(id);
        return Result.success(student);
    }

    @PutMapping("/violation/{id}/{score}")
    public Result violationUpdate(@PathVariable Integer id, @PathVariable Integer score){
        studentService.violationUpdate(id, score);
        return Result.success();
    }
}
